package com.chijun.mform.field;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.FileProvider;

import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.chijun.mform.create.control.FieldAddOptionView;
import com.chijun.mform.create.fragment.FormAddProcessFragment;
import com.chijun.mform.create.mode.FormConfig;
import com.chijun.mform.create.mode.FormField;

import com.chijun.mform.full.mode.ModeFormFile;
import com.chijun.mform.full.mode.ModeFormFullFileData;
import com.chijun.mform.full.mode.ModeFormOrdinaryFieldData;
import com.chijun.mform.full.IHandleWriterData;
import com.chijun.mform.full.mode.ModeLoadServerFormConfigData;
import com.chijun.mform.process.IHandlerLoadFormFull;
import com.chijun.mform.process.LocalConfig;
import com.chijun.mform.process.ModeServerFullData;
import com.chijun.mform.process.OptionChangeListener;
import com.example.ccj.b.c.custom.MEditTextView;
import com.example.ccj.b.c.custom.MImageView;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.b.c.custom.ShowDashLineView;
import com.example.ccj.b.c.custom.TitleBarOnDisplayControl;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.b.c.dialogfragment.scancode.IScanCodeListener;
import com.example.ccj.b.c.dialogfragment.scancode.ScanCodeDialogFragment;
import com.example.ccj.b.c.ui.install.BoMa;
import com.example.ccj.b.c.ui.install.InstallDragSelect;
import com.example.ccj.b.c.ui.install.InstallSelect;
import com.example.ccj.b.c.ui.install.InstallSelectColor;
import com.example.ccj.b.c.ui.install.InstallText;
import com.example.ccj.b.c.ui.install.SelectOnClickListener;
import com.example.ccj.b.c.ui.install.SwitchCheckBoxControl;

import com.example.ccj.u.NumberUtils;
import com.example.ccj.u.SPUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.power.PowerPer;
import com.example.ccj.u.sqlite.SqliteExcuteCommand;
import com.example.ccj.u.tengxun.cos.COS;
import com.example.ccj.u.tengxun.cos.CosDeleteCallBack;
import com.example.ccj.u.tengxun.cos.CosUploadCallBack;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.ObservableEmitter;

public abstract class BaseField {

    public BaseField(){
        formFileModeMap = new HashMap<>();
        formFileModeList = new ArrayList<>();
    }


    //表单填写的相关参数
    //参与计算字段文本改变消息回调码
    public static final int JI_SUAN_GONG_SHI = 12314;       //计算公式回调
    protected Map<String, ModeFormFile> formFileModeMap;
    public List<ModeFormFile> formFileModeList;          //文件字段的数据

    protected List<ModeFormFullFileData> processAddFormFullFileDataList; //审批过程添加的文件
    protected List<ModeFormFullFileData> processDeleteFormFullFileDataList; //审批过程删除的文件
    protected Map<Long,String> processModifyFormFieldDataMap; //审批过程修改的字段



    public View getFieldInstallView(Context context, FormConfig formConfig, FormField formField, ObservableEmitter<ViewGroup> emitter){
        InstallClass installClass = new InstallClass(context, formField, formConfig, emitter);
        return getFieldInstallView(context, installClass);
    }

    //字段设置view，子类实现
    protected abstract View getFieldInstallView(Context context,InstallClass installClass);

    protected static class InstallClass{
        public InstallClass(Context context, FormField formField,FormConfig formConfig,ObservableEmitter<ViewGroup> emitter){
            this.context = context;
            this.formField = formField;
            this.formConfig = formConfig;
            this.emitter = emitter;
            getDefaultView();
        }

        public ObservableEmitter<ViewGroup> emitter;
        public Context context;
        public FormConfig formConfig;
        public FormField formField;
        public FrameLayout frameLayout;
        public View v;

        public void getDefaultView(){
            v = LinearLayout.inflate(context,R.layout.view_field_install_default,null);
            frameLayout = v.findViewById(R.id.view_field_install_default_content);
            emitter.onNext(getNameConfigView());    //字段名称
            emitter.onNext(getHintConfigView());    //字段提示
        }

        /**
         * 字段名称
         * @return 字段名称设置view
         */
        public ViewGroup getNameConfigView(){
            InstallText name = new InstallText("标题", formField.getName(), s -> formField.setName(s));
            name.setMaxLength(255);

            return new SwitchCheckBoxControl(context, name);
        }

        /**
         * 字段提示
         * @return 字段提示设置view
         */
        public ViewGroup getHintConfigView(){
            InstallText hint = new InstallText("提示文字", formField.getHint(), s -> formField.setHint(s));
            hint.setMaxLength(255);
            return new SwitchCheckBoxControl(context,hint);
        }

        /**
         * 默认值
         * @return 字段提示设置view
         */
        public ViewGroup getDefaultContentConfigView(){
            return new SwitchCheckBoxControl(context,new InstallText("默认值", formField.getDefaultContent(), s -> formField.setDefaultContent(s)));
        }

        /**
         * 单位
         * @return 字段单位设置view
         */
        public ViewGroup getCompanyConfigView(){
            InstallText company = new InstallText("单位", formField.getCompany(), s -> formField.setCompany(s));
            company.setMaxLength(30);
            return new SwitchCheckBoxControl(context,company);
        }

        /**
         * 日期类型设置
         * @return 字段单位设置view
         */
        public ViewGroup getTimeTypeConfigView(){
            InstallSelect select = new InstallSelect("日期类型", formField.getTimeType(), 0, new SelectOnClickListener() {
                @Override
                public String format(Object o) {
                    return o.toString();
                }

                @Override
                public String change(Object o) {
                    formField.setTimeType(o.toString());
                    return o.toString();
                }
            });
            select.setOptionList(FormBuilder.getInstance().getTimeType());
            return new SwitchCheckBoxControl(context,select);
        }
        /**
         * 是否必填
         * @return 拨码view
         */
        public ViewGroup getIsMastView(){
            return new SwitchCheckBoxControl(context,new BoMa("是否必填", formField.isMast(), b -> formField.setMast(b)));
        }
        /**
         * 是否必须网络获取
         * @return 拨码view
         */
        public ViewGroup getIsMastInternetGetView(){
            return new SwitchCheckBoxControl(context,new BoMa("必须获取不允许填写", formField.isReadOnly(), b -> formField.setReadOnly(b)));
        }
        /**
         * 是否只读
         * @return 拨码view
         */
        public ViewGroup getIsReadOnlyView(){
            return new SwitchCheckBoxControl(context,new BoMa("是否只读", formField.isReadOnly(), b -> formField.setReadOnly(b)));
        }
        /**
         * 是否显示大写
         * @return 拨码view
         */
        public ViewGroup getIsShowBigView(){
            return new SwitchCheckBoxControl(context,new BoMa("是否显示大写", formField.isShowBig(), b -> formField.setShowBig(b)));
        }
        /**
         * 文本颜色
         * @return 选择文本颜色view
         */
        public ViewGroup getTextColorView(){
            return new SwitchCheckBoxControl(context,new InstallSelectColor("文本颜色", formField.getTextColor(), i -> formField.setTextColor(i)));
        }
        /**
         * 是否可扫码
         * @return 拨码view
         */
        public ViewGroup getIsCanScanView(){
            return new SwitchCheckBoxControl(context,new BoMa("是否可扫码", formField.isCanScanCode(), b -> formField.setCanScanCode(b)));
        }
        /**
         * 选项设置
         * @return 选项设置view
         */
        public ViewGroup getOptionView(){
            FieldAddOptionView fieldAddOptionView = new FieldAddOptionView(context);
            fieldAddOptionView.setTitleName("添加选项");
            fieldAddOptionView.setData(formField, formConfig);
            return fieldAddOptionView;
        }

        public ViewGroup getNumberView(){
            InstallText installItem = new InstallText("小数位数", String.valueOf(formField.getLittleNum())  , s -> formField.setLittleNum(NumberUtils.strToInt(s)));
            installItem.setTextType(InstallText.TextType.number);
            installItem.setMaxLength(2);
            return new SwitchCheckBoxControl(context,installItem);
        }

        public ViewGroup getIsPrintView(){
            return new SwitchCheckBoxControl(context,new BoMa("参与打印", formField.isPrint(), b -> formField.setPrint(b)));
        }

        public ViewGroup getMastShootView(){
            return new SwitchCheckBoxControl(context,new BoMa("必须拍摄", formField.isMastShoot(), b -> formField.setMastShoot(b)));
        }

        public ViewGroup getSelectNumView(){
            int de = formField.getLittleNum();
            if (de < 1) de = 10;
            return new SwitchCheckBoxControl(context,new InstallDragSelect("最大数量", de, 1, 100, i -> formField.setLittleNum(i)));
        }
    }


    public View getShowFullView(Context context, FormField formField,
                                ModeServerFullData serverFullData,
                                OptionChangeListener optionChangeListener,
                                IHandlerLoadFormFull iHandlerLoadFormFull) {
        FullClass fullModel = new FullClass(context, formField, serverFullData);
        fullModel.optionChangeListener = optionChangeListener;
        fullModel.iHandlerLoadFormFull = iHandlerLoadFormFull;
        return getShowFullView(context,fullModel);
    }

    protected View getShowFullView(Context context,  FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //设置字段值
        fullModel.setContentProperty();
        fullModel.loadContent.setMinHeight(200);
        return fullModel.layout;
    }

    /**
     * 加载表单填写的数据的类
     */
    protected class FullClass {

        public Context context;
        public FormField formField;
        public ModeServerFullData serverFullData;
        public OptionChangeListener optionChangeListener;
        public IHandlerLoadFormFull iHandlerLoadFormFull;
        public EditText loadContent;
        public TitleBarOnDisplayControl loadFullDataTitle;
        public FrameLayout frameLayout;
        public View layout;
        public LocalConfig localConfig;
        public int titleTextSize = 13;

        /**
         * 构造
         * @param context 上下文
         * @param formField 字段配置
         * @param serverFullData 需要的属性
         */
        public FullClass(Context context, FormField formField, ModeServerFullData serverFullData) {
            this.context = context;
            this.formField = formField;
            this.serverFullData = serverFullData;
            processAddFormFullFileDataList = new ArrayList<>();
            processDeleteFormFullFileDataList = new ArrayList<>();
            processModifyFormFieldDataMap = new HashMap<>();
            localConfig = new LocalConfig();
            getLoadFormFullDataView();
        }

        private void getLoadFormFullDataView() {
            layout = LinearLayout.inflate(context, R.layout.view_load_form_full_data, null);
//            loadFieldName = layout.findViewById(R.id.load_field_name_textView);
//            loadJumpImageview = layout.findViewById(R.id.load_jumpImageView);
            loadContent = layout.findViewById(R.id.load_input_editText);
            frameLayout = layout.findViewById(R.id.load_custom_control_container);
            loadFullDataTitle = layout.findViewById(R.id.load_full_data_title);

            loadFullDataTitle.getTvTitleView().setTextSize(localConfig.field_name_text_size);
            loadFullDataTitle.getTvLeftTag().setBackgroundColor(localConfig.leftColor);
            setPeiSe();
        }

        //设置字段名和字段值的配色
        private void setPeiSe() {
//            loadFieldName.setTextColor(localConfig.load_color_field_name_text);

            loadFullDataTitle.getTvLeftTag().setBackgroundColor(localConfig.leftColor);
            loadContent.setTextColor(localConfig.load_color_field_content_text);

            if (isHaveModifyPower()) {
                loadFullDataTitle.getTvLeftTag().setBackgroundColor(localConfig.field_have_modify_power_left_tag_color);
            } else {
                loadFullDataTitle.getTvTitleView().setTextColor(localConfig.load_color_field_name_text);
            }
        }

        //默认的设置字段名view属性
        public void setFieldProperty(){
            //字段
            if (formField.isMast()) loadFullDataTitle.setTag("*");
            loadFullDataTitle.setTitle(formField.getName());
        }

        //默认的设置字段值view属性
        public void setContentProperty(){
            String fieldValue = getFieldValue();
            if (StringUtils.isNoEmpty(fieldValue)) {
                loadContent.setText(fieldValue);
            }

            loadContent.setEnabled(false);
            loadContent.setTextIsSelectable(true);//可选择
//            if (!StringUtils.isEmpty(formField.getHint())) loadContent.setHint(formField.getHint());
            if (isHaveModifyPower()) {

                loadContent.setTextColor(localConfig.load_color_field_content_text);
                setJumpImageProperty(R.drawable.field_modify, v -> {
                    loadContent.setTextColor(localConfig.load_color_field_content_can_modify_text);
                    loadContent.setEnabled(true);
                    loadContent.setFocusable(true);
                    loadContent.requestFocus();
                });
                loadContent.setOnFocusChangeListener((v, hasFocus) -> {
                    if (!hasFocus) {
                        loadContent.setTextColor(localConfig.load_color_field_content_text);
                        loadContent.setEnabled(false);
                    } else {
                        loadContent.setSelection(loadContent.length());
                    }
                });

                loadContent.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {}
                    @Override
                    public void afterTextChanged(Editable s) {
                        processModifyFormFieldDataMap.put(formField.getId(),s.toString());
                    }
                });
            }
        }

        //隐藏字段数据填写框view
        public void goneContentView(){
            loadContent.setVisibility(View.GONE);
        }
        //隐藏字段名view
        public void goneFieldNameView(){
            loadFullDataTitle.setVisibility(View.GONE);
        }

        /**
         * 设置右侧图标的属性
         * @param img 图标资源
         * @param onClickListener 点击事件
         */
        public void setJumpImageProperty(int img, View.OnClickListener onClickListener){
            if (isHaveModifyPower()) loadFullDataTitle.setImageOnclick(img,onClickListener);
        }

        /**
         * 判断是否有修改权限
         * @return true 有
         */
        public boolean isHaveModifyPower(){

            return formField.getPermission() == FormAddProcessFragment.MODIFY_POWER_HAVE;
        }

        /**
         * 获取该字段的值
         * @return 字段值列表
         */
        public String getFieldValue(){
            //获取
            return serverFullData.formOrdinaryFieldDataJsonObject.getString(String.valueOf(formField.getId()));
        }

        /**
         * 放入自定义view
         * @param customView 自定义view
         */
        public void addCustomControl(View customView) {
            frameLayout.addView(customView);
        }

        protected boolean getIsCanModify(){
            return true;
        }
        public Context getContext() {
            return context;
        }
        public FormField getFormField() {
            return formField;
        }
        public ModeServerFullData getServerFullData() {
            return serverFullData;
        }
        public EditText getLoadContent() {
            return loadContent;
        }
        public FrameLayout getFrameLayout() {
            return frameLayout;
        }
    }


    /**
     * 填写表单的字段view
     * @param context 上下文
     * @param formField 字段
     * @param writeFormData 数据
     * @param iHandleWriterData 服务器数据回调
     * @param optionChangeListener 选项改变回调
     * @return 字段view
     */
    public View getWritelView(Context context,
                              FormField formField,
                              ModeLoadServerFormConfigData writeFormData,
                              IHandleWriterData iHandleWriterData,
                              OptionChangeListener optionChangeListener){
        WriteFormClass writeFormClass = new WriteFormClass(context, formField, writeFormData);
        writeFormClass.iHandleWriterData = iHandleWriterData;
        writeFormClass.optionChangeListener = optionChangeListener;
        return getWritelView(context, writeFormClass);
    }

    //加载表单填写的view，子类实现
    protected abstract View getWritelView(Context context, WriteFormClass writeFormClass);

    /**
     * 填写表单加载配置的类
     */
    public class WriteFormClass {

        //表单填写的相关参数
        public Context baseContext;                          //上下文
        public IHandleWriterData iHandleWriterData;          //表单填写回调
        public ModeLoadServerFormConfigData writeFormData;   //数据
        public OptionChangeListener optionChangeListener;    //选项字段选项更改回调

        public FormField formField;                          //字段配置

        public boolean inputIsOK = false;                    //是否填写

        //表单填写加载相关view定义
        public MTextView fieldFullNameView, fieldFullWriteErrorView;     //字段名，字段填写错误提示
        public ImageView fieldFullMast;                                  //是否必填图片
        public MEditTextView fieldFullContentView;                       //填写控件
        public MImageView fieldFullJumpImgView;                          //跳转图片
        public ShowDashLineView showDashLineView;
        public LinearLayout fieldFullContentLayout;                      //填写主控件

        public View view;

        public int leftColor  = Color.parseColor("#DDDDDD");
        public int titleTextSize = 13;

        public WriteFormClass(Context context, FormField formField, ModeLoadServerFormConfigData writeFormData){
            this.baseContext = context;
            this.formField = formField;
            this.writeFormData = writeFormData;
            this.view = getDefaultFieldFullView();
        }

        //加载表单填写的view，子类实现
        public View getFieldFullView(){
            return getDefaultFieldFullView();
        }

        //表单填写默认view
        protected View getDefaultFieldFullView(){
            View view = LinearLayout.inflate(baseContext, R.layout.view_field_load_type_1, null);
            fieldFullNameView = view.findViewById(R.id.view_load_field_name);
            fieldFullWriteErrorView = view.findViewById(R.id.view_load_field_error_hint);
            fieldFullJumpImgView = view.findViewById(R.id.view_load_field_jump_img);
            fieldFullContentView = view.findViewById(R.id.view_load_field_content);
            fieldFullContentLayout = view.findViewById(R.id.view_load_field_content_layout);
            fieldFullMast = view.findViewById(R.id.view_load_field_mast);
            showDashLineView = view.findViewById(R.id.view_load_field_error_dash);
            fieldFullNameView.setText(formField.getName());
            return view;
        }

        public void goneErrorView(){
            fieldFullWriteErrorView.setVisibility(View.GONE);
        }

        public void setError(String value, int color){
            fieldFullWriteErrorView.setVisibility(View.VISIBLE);
            fieldFullWriteErrorView.setText(value);
            if (color != 0) fieldFullWriteErrorView.setTextColor(color);
        }
        public IHandleWriterData getiHandleWriterData() {
            return iHandleWriterData;
        }
        public void setIHandleWriterData(IHandleWriterData iHandleWriterData) {
            this.iHandleWriterData = iHandleWriterData;
        }


        /**
         * 获取图片的文件上传模型
         *
         * @param localMedia 图片
         * @return 文件上传模型
         */
        public ModeFormFile getFileMode(Context context, LocalMedia localMedia){
            return getFileMode(context,new File(localMedia.getRealPath()));
        }

        /**
         * 获取文件的上传模型
         * @param context 上下文
         * @param file 文件
         * @return 文件上传模型
         */
        public ModeFormFile getFileMode(Context context, File file){
            return BaseField.this.getFileMode(context, "form", file, writeFormData.form.id, writeFormData.id, formField.getId());
        }

        /**
         * 上传单个文件
         * @param localMedia 图片
         * @param upLoadFileListener 文件上传完毕回调
         */
        public void functionSaveImage(Context context,LocalMedia localMedia, UpLoadFileListener upLoadFileListener){
            List<LocalMedia> files = new ArrayList<>();
            files.add(localMedia);
            functionSaveImage(context,files,upLoadFileListener);
        }

        /**
         * 上传单个文件
         * @param localMedia 图片列表
         * @param upLoadFileListener 文件上传完毕回调
         */
        public void functionSaveImage(Context context,List<LocalMedia> localMedia,UpLoadFileListener upLoadFileListener){
            List<ModeFormFile> fileModeList = new ArrayList<>();
            for (LocalMedia media : localMedia) {
                ModeFormFile form = getFileMode(context,media);
                fileModeList.add(form);
            }
            functionSaveFileMode(context,fileModeList,upLoadFileListener);
        }

        /**
         * 上传单个文件
         * @param file 文件
         * @param upLoadFileListener 文件上传完毕回调
         */
        public void functionSaveFile(Context context,File file, UpLoadFileListener upLoadFileListener){
            List<File> files = new ArrayList<>();
            files.add(file);
            functionSaveFile(context,files,upLoadFileListener);
        }

        /**
         * 上传多个文件
         * @param fileList 文件数组
         * @param upLoadFileListener 文件上传完毕回调
         */
        public void functionSaveFile(Context context, List<File> fileList, UpLoadFileListener upLoadFileListener){
            if (!fileList.isEmpty()) {
                List<ModeFormFile> fileModeList = new ArrayList<>();
                for (int i = 0; i < fileList.size(); i++) {
                    File file = fileList.get(i);
                    ModeFormFile fileMode = getFileMode(context,file);
                    fileModeList.add(fileMode);
                }
                functionSaveFileMode(context,fileModeList,upLoadFileListener);
            }
        }
    }



    protected ModeFormFile getFileMode(Context context, String fileTypeStr, File file, long yiJi, long erJi , long sanJi){
        ModeFormFile formImageMode = new ModeFormFile();
        formImageMode.yiJiId = yiJi;
        formImageMode.erJiId = erJi;
        formImageMode.sanJiId = sanJi;
        formImageMode.url =  "/" + fileTypeStr + "/" + yiJi + "/" + erJi + "/" + SPUtils.getInstance().getString("user_id") + "/zd/" + sanJi  +"/" +file.getName();
        formImageMode.uri = FileProvider.getUriForFile(context, "com.ccj.tool.fileProvider", file).toString();
        formImageMode.name = file.getName();
        formImageMode.path = file.getPath();
        return formImageMode;
    }

    protected ModeFormFullFileData getFileFullMode(Context context, String fileTypeStr, File file,  long yiJi, long erJi , long sanJi){
        ModeFormFullFileData formFullFileData = new ModeFormFullFileData();
        formFullFileData.path = file.getPath();
        formFullFileData.rq = FormBuilder.RONG_QI;
        formFullFileData.bucket = FormBuilder.BUCKET_NAME;
        formFullFileData.dy = FormBuilder.DI_YU;
        formFullFileData.reOne = yiJi;
        formFullFileData.reTwo = erJi;
        formFullFileData.reThree = sanJi;

        return formFullFileData;
    }


    int upLoadNum = 0;

    /**
     * 上传单个文件
     * @param formFileMode 文件
     * @param upLoadFileListener 文件上传完毕回调
     */
    protected void functionSaveFile(Context context, ModeFormFile formFileMode, UpLoadFileListener upLoadFileListener){
        List<ModeFormFile> files = new ArrayList<>();
        files.add(formFileMode);
        functionSaveFileMode(context,files,upLoadFileListener);
    }

    /**
     * 上传多个文件
     * @param formFileModeList 文件数组
     * @param upLoadFileListener 上传完毕回调
     */
    protected void functionSaveFileMode(Context context, List<ModeFormFile> formFileModeList, UpLoadFileListener upLoadFileListener){
        LoadingDialog loadingDialog = new LoadingDialog(context);
        loadingDialog.showUpload(formFileModeList.size()*100);
        upLoadNum = 0;
        for (ModeFormFile fileMode : formFileModeList) {
            loadingDialog.setUploadText(fileMode.name);
            String realPath = fileMode.path;
            String fileName = fileMode.url;

            COS.getInstance().uploadFile(context, FormBuilder.BUCKET_NAME, fileName, realPath, null, new CosUploadCallBack() {
                @Override
                public void onSleep(double v) {
                    loadingDialog.setSpeed((int) (upLoadNum * 100 + v));
                    if (v>=100) upLoadNum ++;
                }
                @Override
                public void onComplete(String s) {
                    upLoadFileListener.onComplete(fileMode,new File(fileMode.path),LocalMedia.generateLocalMedia(context,fileMode.path));
                    formFileModeMap.put(fileMode.name,fileMode);

                }
                @Override
                public void onError(String s) {
                    ToastUtils.showShort(s);
                    loadingDialog.dismiss();
                }
            });
        }
    }


    /**
     * 删除图片功能
     */
    protected void functionDeleteImg(Context context,String path,CosFileDeleteListener listener){
        try {
            COS.getInstance().deleteFile(context, FormBuilder.BUCKET_NAME, path, new CosDeleteCallBack() {
                @Override
                public void onComplete(String s) {
                    listener.onSuccess(s);
                    for (ModeFormFile formFileMode : formFileModeList) {
                        if (formFileMode.url.equals(s)) {
                            formFileModeList.remove(formFileMode);
                            formFileModeMap.remove(formFileMode.name);
                            listener.onSuccess(formFileMode);
                            break;
                        }
                    }
                }

                @Override
                public void onError(String s) {
                    ToastUtils.showShort(s);
                    listener.onError(s);
                }
            });
        } catch (Exception e) {
            ToastUtils.showShort(e.getMessage());
        }
    }

    protected interface CosFileDeleteListener{
        void onError(String error);
        void onSuccess(String path);
        void onSuccess(ModeFormFile formFileMode);

    }


    /**
     * 获取暂存的文本字段数据
     * @return 1
     */
    protected String getTextFieldZanCunData(Context context,String tj,String... ww) {
        List<ModeFormOrdinaryFieldData> formOrdinaryFieldDataList = SqliteExcuteCommand.get(context, ModeFormOrdinaryFieldData.class, tj, ww);
        if (formOrdinaryFieldDataList != null && !formOrdinaryFieldDataList.isEmpty()) {
            return formOrdinaryFieldDataList.get(0).content;
        }
        return null;
    }

    public CharSequence saveValue;                       //文本类字段填写的数据

    /**
     * 输入验证
     * @return 1
     */
    protected boolean check(CharSequence value, FormField field, TextView textView){
        //验证是否为空
        if (field.isMast() || StringUtils.isEmpty(value)) {
            textView.setVisibility(View.VISIBLE);
            textView.setText("该字段不可为空！");
            return false;
        }
        textView.setVisibility(View.GONE);
        saveValue = value;
        return true;
    }

    /**
     * 扫码功能
     */
    protected void functionSaoMa(Context context,TextView textView){
        if (PowerPer.getScanCodePower(context)) {
            ScanCodeDialogFragment scanCodeDialogFragment = new ScanCodeDialogFragment(context, new IScanCodeListener() {
                @Override
                public void onResult(String s) {
                    if (textView != null) {
                        textView.setText(s);
                    } else {
                        ToastUtils.showShort(s);
                    }
                    saveValue = s;
                }
            });
            scanCodeDialogFragment.show();
        }
    }

    public List<ModeFormFile> getFormFileModeList() {
        return formFileModeList;
    }
    public void setFormFileModeList(List<ModeFormFile> formFileModeList) {
        this.formFileModeList = formFileModeList;
    }


    public CharSequence getSaveValue() {
        return saveValue;
    }

}
